const fs = require('fs-extra');
const glob = require('glob');
const path = require('path');
const prettier = require('prettier');

import { composePagesJson, replaceThemeOptions } from '@core';
import { isCli } from '@core/util';
import { logSuccess, isEmptyObject, PAGE_CONFIG } from '@shared';

import { readRouters, readProfile } from './util';
import { getManifestJson } from '@core/manifest';

const srcPath = isCli() ? 'src/' : '';

export function build(options) {
  options.output = {
    access: {
      filename: 'src/routers/access.js',
    },
    alias: {
      filename: 'src/routers/alias.js',
    },
    pages: {
      filename: 'src/routers/pages.json',
    },
    ...options.output,
  };

  const routers = readRouters();
  if (routers.length) {
    const profile = readProfile();
    const pagesJsonPath = srcPath + 'pages.json';
    const pagesJsonBundle = composePagesJson(routers, profile);
    let pagesJsonString = pagesJsonBundle.pagesJson;
    // 替换主题配置
    pagesJsonString = replaceThemeOptions(pagesJsonString, profile.theme);
    // 其他配置
    const output = options.output || {};
    const access = output.access;
    if (access) {
      const routerAccess = pagesJsonBundle.extras.routerAccess;
      if (!isEmptyObject(routerAccess)) {
        const filePath = path.resolve(process.cwd(), access.filename);
        let accessBundle = `export default ${JSON.stringify(routerAccess)}`;
        accessBundle = prettier.format(accessBundle, {
          tabWidth: 2,
          singleQuote: true,
          parser: 'babel',
        });
        fs.outputFileSync(filePath, accessBundle, 'utf8');
      }
    }
    // const alias = output.alias;
    // if (alias) {
    //   const routerAlias = pagesJsonBundle.extras.routerAlias;
    //   const filePath = path.resolve(process.cwd(), alias.filename);
    //   let aliasBundle = `export default ${JSON.stringify(routerAlias)}`;
    //   aliasBundle = prettier.format(aliasBundle, {
    //     tabWidth: 2,
    //     singleQuote: true,
    //     parser: 'babel',
    //   });
    //   fs.outputFileSync(filePath, aliasBundle, 'utf8');
    // }
    // if (options.app) {
    //   if (typeof options.app === 'string') {
    //     options.app = {
    //       alias: options.app,
    //     };
    //   }
    //   const manifest = getManifestJson();
    //   const routerList = [];
    //   const data = readRouters();
    //   data.forEach((router) => {
    //     const page = {
    //       path: router.path,
    //     };
    //     let using = true;
    //     if (router[PAGE_CONFIG]) {
    //       if (router[PAGE_CONFIG].app) {
    //         page.app = router[PAGE_CONFIG].app;
    //       }
    //       if (router[PAGE_CONFIG].using === false) {
    //         using = false;
    //       }
    //       page.config = router[PAGE_CONFIG];
    //     }
    //     if (using) {
    //       routerList.push(page);
    //     }
    //   });
    //   const filePath = path.resolve(process.cwd(), output.pages.filename);
    //   fs.outputJsonSync(
    //     filePath,
    //     {
    //       app: {
    //         alias: options.app.alias,
    //         appid: manifest['mp-weixin'].appid,
    //       },
    //       pages: routerList,
    //     },
    //     {
    //       spaces: 2,
    //     }
    //   );
    // }
    // 生成公用的配置文件

    fs.outputFileSync(pagesJsonPath, pagesJsonString, 'utf8');

    logSuccess(`vni build [${pagesJsonPath}] success`);

    return true;
  } else {
    //TODO 提示没找到相关文件
    return false;
  }
}
